Currently, I am planning to add a new feature to my app that allows multiple users to collaboratively manage a single legder. Initially, I chose SwiftData with iCloud for development, so I wanted to inquire whether SwiftData currently supports data sharing among multiple users through iCloud. If it does not, should I transition entirely to Core Data, or is it feasible to allow Core Data and SwiftData to work together?
Selecting any option will automatically load the page
Post
Replies
Boosts
Views
Activity
Presently, I am encountering an issue with SwiftData. For instance, I have a SwiftData class Ledger that encompasses an array of SingleTransaction, which is also a SwiftData class.
Here is the question: when I save a Ledger, how can I discern that the .NSManagedObjectContextDidSave notification was triggered by saving the Ledger and not by saving a SingleTransaction? This distinction is crucial to circumvent unnecessary updates. I attempted the following syntax, but Xcode indicates that Cast from NSManagedObject to unrelated type Ledger always fails.
List {...}
.onReceive(
NotificationCenter
.default
.publisher(for: .NSManagedObjectContextDidSave)
.receive(on: DispatchQueue.main),
perform: { notification in
if let userInfo = notification.userInfo,
let updatedObjects = userInfo[NSUpdatedObjectsKey] as? Set<NSManagedObject> {
if updatedObjects.contains(where: { $0 is Ledger }) {
fetchLedgers()
}
}
}
)
What can I do?
I create a simple app to enable user add an agenda to their calendar. I create an EventEditViewController which can be used in my SwiftUI app. When I tap the save button appeared on the sheet, the agenda can be saved to calendar but comes with some Xcode console output:
redactedMimicSaveEvent: could not find corresponding remoteUI user committed permanent objectID for hostTempID[x-apple-eventkit:///Alarm/t6]. initialTempID[x-apple-eventkit:///Alarm/t5]
What does that mean?
I want to try the new TipKit features but when I import it from project, Xcode told me that No such module 'TipKit' and I also try the following code to create a tip directly but it doesn't work and Xcode shows Cannot find type 'Tip'. What can I do to use the new feature?
// MARK: Tips define here.
struct bookmarkTip: Tip {
}
The following grammar is no longer available in Xcode 14, but the Store property is only available in macOS 12.0 +, my app also supports macOS 11.0, and now the compile is error, how can I solve this problem?
struct PayButton: View {
@available(macOS 12.0, *)
@EnvironmentObject var store: Store
...
}
Could anyone here tell me how I can use Live Text API in SwiftUI ? The code provide uses UIKit and not complete, the code is for existing project, but I want to develop a new SwiftUI project, so I have no idea how to implement it.
This is a macOS app, while using Image("my file url") in my CoverImageView, I can export a .png file that contains the Image("my file url") view successfully.
However, this doesn't happen when I use Image(nsImage: NSImage(data: imageData.imagedata)), the imageData.imagedata is a Data type that will be gotten when I tap a button and get it from a selected picture (I will show it later in my code).
When I select an image, it can be seen in my app's View.
However, when I save this View(CoverImageView) to a .png file it only contain the blue view, the Mac view is gone!!!!
Here is the CoverImageView from which I want to create a .png file
struct CoverImageView: View {
@EnvironmentObject var imageData: ImageData
var body: some View {
ZStack {
Rectangle()
.frame(width: 512, height: 600)
.foregroundColor(.blue)
Image(nsImage: (NSImage(data: imageData.imagedata) ?? NSImage(byReferencing: URL(fileURLWithPath: ""))))
.resizable()
.aspectRatio(contentMode: .fit)
.frame(width: 512, height: 512)
}
}
}
This is the main view PhotoTestView
struct PhotoTestView: View {
@State private var imageUrl: URL = URL(fileURLWithPath: "")
@EnvironmentObject var imageData: ImageData
var body: some View {
VStack {
CoverImageView()
Divider()
.frame(width: 1024)
HStack {
Button(action: {
if let openURL = ImageProcess().showOpenPanel() {
imageUrl = openURL
if let codedImages = try? Data(contentsOf: openURL) {
imageData.imagedata = codedImages
}
}
}, label: {
Image(systemName: "doc.badge.plus")
})
Button(action: {
ImageProcess().saveImage()
}, label: {
Image(systemName: "square.and.arrow.down")
})
}.padding()
}
}
}
The View Extension which will create a png file from a view
extension View {
func imageRepresentation(rect: CGRect) -> NSBitmapImageRep? {
let hosting = NSHostingView(rootView: self)
hosting.setFrameSize(rect.size)
hosting.setBoundsSize(rect.size)
hosting.layout()
hosting.layerContentsRedrawPolicy = .onSetNeedsDisplay
hosting.setNeedsDisplay(rect)
if let imageRepresentation = hosting.bitmapImageRepForCachingDisplay(in: rect) {
hosting.cacheDisplay(in: rect, to: imageRepresentation)
return imageRepresentation
}
return nil
}
func asImage(rect: CGRect) -> NSImage? {
if let cgImage = imageRepresentation(rect: rect)?.cgImage {
return NSImage(cgImage: cgImage, size: rect.size)
}
return nil
}
func asPngData(rect: CGRect) -> Data? {
return imageRepresentation(rect: rect)?.representation(using: .png, properties: [:])
}
}
png File Reader and Save
struct ImageProcess {
func showOpenPanel() -> URL? {
let openPanel = NSOpenPanel()
openPanel.allowedContentTypes = [.image]
openPanel.allowsMultipleSelection = false
openPanel.canChooseDirectories = false
openPanel.canChooseFiles = true
let response = openPanel.runModal()
return response == .OK ? openPanel.url : nil
}
func saveURL() -> URL? {
let savePanel = NSSavePanel()
savePanel.allowedContentTypes = [.png]
savePanel.canCreateDirectories = true
savePanel.isExtensionHidden = false
savePanel.allowsOtherFileTypes = false
savePanel.title = "Save your image"
savePanel.message = "Choose a folder and a name to store your image."
savePanel.nameFieldLabel = "File name:"
let response = savePanel.runModal()
return response == .OK ? savePanel.url : nil
}
func saveImage() {
let view = CoverImageView().environmentObject(ImageData())
let imageData = view.asPngData(rect: CGRect.init(x: 0, y: 0, width: 1024, height: 768))
if let url = saveURL() {
try? imageData!.write(to: url)
}
// print(imageData)
}
}
Could you help me?